Ubuntu20.04静态ip和dhcp配置

您所在的位置:网站首页 ubuntu ip地址设置 Ubuntu20.04静态ip和dhcp配置

Ubuntu20.04静态ip和dhcp配置

2023-03-19 12:59| 来源: 网络整理| 查看: 265

转载,阅读原文:https://www.cnblogs.com/JaxYoun/p/13140095.html

一、目录及配置文件 cd /etc/netplan vi 01-network-manager-all.yaml 二、静态ip配置方式

01-network-manager-all.yaml.static

# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: enp4s0: dhcp4: false addresses: [192.168.100.87/24] gateway4: 192.168.100.3 nameservers: addresses: [192.168.100.3,8.8.8.8] 三、DHCP配置方式

01-network-manager-all.yaml.dhcp

# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: enp4s0: dhcp4: true 四、重启netplan

需要启用哪一种ip分配策略就拷贝文件,并去掉最后的后缀

cp 01-network-manager-all.yaml.xxxx 01-network-manager-all.yaml netplan --debug apply ip add动态IP变化时,使用 netplan apply 即可刷新IP

 

其他参考 https://linux265.com/news/6376.html   https://zh.codepre.com,有点乱:

Ubuntu 17.10和更高版本使用netplan作为默认网络管理工具。以前的Ubuntu版本使用ifconfig及其配置文件/etc/network/interfaces来配置网络。

netplan配置文件以YAML语法编写,扩展名为.yaml。要使用netplan配置网络接口,需要为该接口创建YAML描述,并且netplan将为所选的渲染器工具生成所需的配置文件。

netplan支持两个渲染器:NetworkManager和Systemd-networked。NetworkManager通常在台式机上使用,而Systemd网络在没有GUI的服务器上使用。

在Ubuntu服务器上配置静态IP地址在Ubuntu 20.04上,系统使用“可预测的网络接口名称”标识网络接口。

设置静态IP地址的第一步是识别要配置的以太网接口的名称,使用 ip link 命令打印所有可用网络接口的列表,常见的接口名称有eth0、ens3:1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:002: ens3: mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff

netplan配置文件存储在/etc/netplan目录中,可能有一个或多个YAML文件,文件的名称可能因安装程序而异。通常,该文件名为01-netcfg.yaml、00-installer-config.yaml、50-cloud-init.yaml或NN_interfaceName.yaml,但是在不同系统中可能有所不同。

如果是Ubuntu云实例,可能配置有cloud-init,则需要将其禁用。为此,请创建以下文件:vi /etc/cloud/cloud.cfg.d/99-disable-network-config.cfgnetwork: {config: disabled}

要在网络接口上配置静态IP地址,编辑YAML配置文件 /etc/netplan/01-netcfg.yaml:network:  version: 2  renderer: networkd  ethernets:    ens3:      dhcp4: yes # 或设置为true

在更改配置之前,简要说明一下代码。

每个netplan Yaml文件均以具有至少两个必需元素的网络密钥开头。第一个必需元素是网络配置格式的版本,第二个是设备类型。设备类型可以是以太网,绑定,网桥或VLAN。

上面的配置还有一行显示渲染器类型的行。开箱即用,如果在服务器模式下安装Ubuntu,则渲染器配置为使用networkd作为后端。

在设备的类型(以太网)下,可以指定一个或多个网络接口。在此示例中,只有一个接口ens3,该接口被配置为从DHCP服务器dhcp4获取IP地址:是。

要将静态IP地址分配给ens3接口,编辑文件/etc/netplan/01-netcfg.yaml:network:  version: 2  renderer: networkd  ethernets:    ens3:      dhcp4: no      addresses:        - 192.168.121.221/24      gateway4: 192.168.121.1      nameservers:        addresses: [8.8.8.8, 1.1.1.1]

将DHCP设置为dhcp4:否。指定静态IP地址。在地址下:可以添加一个或多个将分配给网络接口的IPv4或IPv6 IP地址。指定网关。在名称服务器下,设置名称服务器的IP地址。编辑Yaml文件时,请确保遵循YAML代码缩进标准。如果语法不正确,则更改将不会应用。完成后,通过运行以下命令保存文件并应用更改:netplan apply

校验修改是否生效ip addr show dev ens32: ens3: mtu 1500 qdisc mq state UP group default qlen 1000    link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff    inet 192.168.121.221/24 brd 192.168.121.255 scope global dynamic ens3        valid_lft 3575sec preferred_lft 3575sec    inet6 fe80::5054:ff:feb0:f500/64 scope link         valid_lft forever preferred_lft forever

示例:

1. 配置DHCP自动获取编辑 /etc/netplan/00-installer-config.yaml_auto# This is the network config written by 'subiquity'network:  ethernets:    eth0:      dhcp4: true  version: 2

2. 配置静态IP编辑 /etc/netplan/00-installer-config.yaml# This is the network config written by 'subiquity'network:  ethernets:    eth0:      dhcp4: no      dhcp6: no      # Set IP address & subnet mask      addresses:      - 172.17.140.90/28      # Set default gateway      gateway4: 172.17.140.81      nameservers:        # Set DNS name servers        addresses:        - 223.5.5.5        - 8.8.8.8  version: 2

重启网络:netplan apply

如果不需要IPv6,可以禁用:echo “net.ipv6.conf.all.disable_ipv6 = 1” | tee -a /etc/sysctl.confsysctl -p 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3